<template>
<master :title="title">
  <listpage ref="listpage"></listpage>
</master>
</template>
<script type="text/javascript">
(function (G) {
  G.vue({
  "usingComponents": {
    "master": "/components/Master/Master",
    "listpage": "/components/List/Page/Page"
  },
  "enablePullDownRefresh": true
},{
    data: {
      title: '',
      modelName: '',
      nickName:'',
      models: {},
      platform: G.platform,
      type:''
    },
    methods: {
      load: function (next) {
        this.selectComponent('#listpage').load(next);
      },
      refresh: function (next) {
        this.selectComponent('#listpage').refresh(next);
      },
      onPullDownRefresh: function (event) {
        this.refresh(function () {
          G.stopPullDownRefresh();
        })
      },
      onReachBottom: function (event) {
        this.load();
      },
      edit: function (event) {
        var modeleditquery = {
          values: {},
          refresh: this.refresh,
          models: this.getData('models'),
          url: '/api/model/configuration'
        }
        let type = this.getData('type')
        if (event.type == 'edit') {
          modeleditquery.values = event.detail;
          modeleditquery.title = '修改 ' + event.detail.name + ' ' + this.getData('nickName');
          modeleditquery.meth = 'PUT';
          modeleditquery.models.id.readOnly = true;
          modeleditquery.models.type.readOnly = true;
          if(type){
            modeleditquery.models.type.type = '';
          }
        } else {
          modeleditquery.title = '新增 ' + this.getData('nickName');
          modeleditquery.meth = 'POST';
          modeleditquery.models.type.rule = modeleditquery.models.type.rule || {};
          modeleditquery.models.type.rule.Regx = '^(?!sys_)';
          if(type){
            modeleditquery.values.type = type;
            modeleditquery.models.type.type = '';
          }
        }
        G.Storage.set('modeleditquery', modeleditquery);
        this.$go('/pages/model/edit/edit');
      }
    },
    mounted: function (query) {
      query.nickName = query.nickName || G.Storage.get('nickNames').configuration || '配置'
      if(query.type) this.setData({type:query.type})
      var title = query.nickName;
      var _this = this;
      G.ajax('/model/configuration', function (ret) {
        if (!ret.errorMsg) {
          ret.value.model = function(values,model){
            return values.model ? G.string(values.model).parse() : '';
          }
          let getUrl = '/api/model/configuration'
          if(query.type) {
            getUrl += '?type=' + query.type
            ret.type.unList = true
          }
          _this.selectComponent('#listpage').init({
            getUrl,
            deleteUrl: '/api/model/configuration',
            models: ret,
            searchKey: query.type ? '' : (query.searchKey || 'type'),
            canCheck:false,
            edit:{
              status:'/api/model/configuration'
            },
            actions:{
              add:{
                name:'新增',
                action: function (event) {
                  _this.edit(event);
                }
              },
              edit:{
                name:'修改',
                action: function (event) {
                  _this.edit(event);
                }
              }
            }
          })
          _this.load();
          _this.setData({ models: ret,nickName:query.nickName, title: title, modelName: 'configuration' });
        }
      }, {
          storageName: 'configurationModel'
        })
    }
  });
})(Y)
</script>
<style scoped>

</style>